package com.linkvin.ec.hcs.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.linkvin.ec.hcs.dao.model.mdm.bo.VModelBo;
import com.linkvin.ec.hcs.service.mdm.VBrandService;
import com.linkvin.ec.hcs.service.mdm.VModelService;
import com.linkvin.ec.hcs.service.mdm.VSeriesService;
import com.linkvin.ec.hcs.service.util.BaseException;
import com.linkvin.ec.hcs.service.util.enums.DriveModeEnum;
import com.linkvin.ec.hcs.service.util.enums.PowerTypeEnum;
import com.linkvin.ec.hcs.service.util.enums.VehicleAirSuctionEnum;
import com.linkvin.ec.hcs.util.EnumUtil;
import com.linkvin.ec.hcs.vo.EnumVO;
import com.linkvin.ec.hcs.vo.RestVO;
import com.linkvin.ec.hcs.vo.VModelVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping(value = "/vehicle", produces = MediaType.APPLICATION_JSON_VALUE)
public class VModelController {

    @Autowired
    private VModelService vModelService;

    @Autowired
    private VSeriesService vSeriesService;

    @Autowired
    private VBrandService vBrandService;

    private static final Logger LOGGER = LoggerFactory.getLogger(VModelController.class);

    @PostMapping(value = "/queryPage")
    public RestVO<Page<VModelVO>> queryVModelList(@RequestBody Page<VModelBo> page){
        RestVO<Page<VModelVO>> result = new RestVO<>(0,"success");
        LOGGER.info("VModelController.queryVModelList params : {}", page);
        try{
            Page<VModelVO> pageVModeVO = new Page<>();

            Map<String,Object> condition = page.getCondition();
            String mode = condition.get("mode").toString();
            List<VModelBo> listVModelBo = Lists.newArrayList();
            if(mode.equals("allbrands")){
                listVModelBo = vBrandService.queryPageVBrand(page);
            }

            if(mode.equals("brand")){
                listVModelBo = vSeriesService.queryPageVSeries(page);
            }

            if(mode.equals("series")){
                listVModelBo = vModelService.queryPageVVehicle(page);
            }

            BeanUtils.copyProperties(page, pageVModeVO);
            List<VModelVO> listVModelVo = Lists.newArrayList();

            listVModelBo.forEach(vModelBo -> {
                VModelVO vModelVO = new VModelVO();
                BeanUtils.copyProperties(vModelBo,vModelVO);
                if(!Strings.isNullOrEmpty(vModelVO.getAirSuction())){
                    vModelVO.setAirSuction(VehicleAirSuctionEnum.getByValue(vModelVO.getAirSuction()).getText());
                }
                if(!Strings.isNullOrEmpty(vModelVO.getPowerType())){
                    vModelVO.setPowerType(PowerTypeEnum.getByValue(vModelVO.getPowerType()).getText());
                }
                if(!Strings.isNullOrEmpty(vModelVO.getDriveMode())){
                    vModelVO.setDriveMode(DriveModeEnum.getByValue(vModelVO.getDriveMode()).getText());
                }
                listVModelVo.add(vModelVO);
            });

            pageVModeVO.setRecords(listVModelVo);
            result.setData(pageVModeVO);

        }catch(Exception ex)
        {
            LOGGER.error("VModelController.queryVModelList ERROR Message : {} ", ex.getMessage());
            result.setCode(30004);
            result.setMessage("车型查询错误");
        }

        return result;
    }


    @PostMapping(value = "/create")
    public RestVO<VModelVO> create(@RequestBody VModelBo vModelBo){
        RestVO<VModelVO> result = new RestVO<>(0,"success");
        LOGGER.info("VModelController.create params : {}", vModelBo);
        try {
            //非空校验
            if (Strings.isNullOrEmpty(vModelBo.getModelName().trim())) throw new BaseException("车型名称不能为空");
            if (Objects.equals(vModelBo.getSeriesId(),null)) throw new BaseException("所属车系不能为空");

            VModelVO vModelVO =new VModelVO();
            int brandID = vSeriesService.getBrandIdBySeriesId(vModelBo.getSeriesId());
            vModelBo.setBrandId(brandID);
            vModelBo = vModelService.addModel(vModelBo);
            BeanUtils.copyProperties(vModelBo,vModelVO);
            if(!Strings.isNullOrEmpty(vModelVO.getAirSuction())){
                vModelVO.setAirSuction(VehicleAirSuctionEnum.getByValue(vModelVO.getAirSuction()).getText());
            }
            if(!Strings.isNullOrEmpty(vModelVO.getPowerType())){
                vModelVO.setPowerType(PowerTypeEnum.getByValue(vModelVO.getPowerType()).getText());
            }
            if(!Strings.isNullOrEmpty(vModelVO.getDriveMode())){
                vModelVO.setDriveMode(DriveModeEnum.getByValue(vModelVO.getDriveMode()).getText());
            }
            result.setData(vModelVO);
        }catch (Exception e){
            LOGGER.error("VModelController.create ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/update")
    public RestVO<VModelVO> update(@RequestBody VModelBo vModelBo){
        RestVO<VModelVO> result = new RestVO<>(0,"success");
        LOGGER.info("VModelController.update params : {}", vModelBo);
        try {
            //非空校验
            if (Strings.isNullOrEmpty(vModelBo.getModelName().trim())) throw new BaseException("车型名称不能为空");
            if (Objects.equals(vModelBo.getSeriesId(),null)) throw new BaseException("所属车系不能为空");

            VModelVO vModelVO =new VModelVO();
            int brandID = vSeriesService.getBrandIdBySeriesId(vModelBo.getSeriesId());
            vModelBo.setBrandId(brandID);
            vModelBo = vModelService.updateModel(vModelBo);
            BeanUtils.copyProperties(vModelBo,vModelVO);
            if(!Strings.isNullOrEmpty(vModelVO.getAirSuction())){
                vModelVO.setAirSuction(VehicleAirSuctionEnum.getByValue(vModelVO.getAirSuction()).getText());
            }
            if(!Strings.isNullOrEmpty(vModelVO.getPowerType())){
                vModelVO.setPowerType(PowerTypeEnum.getByValue(vModelVO.getPowerType()).getText());
            }
            if(!Strings.isNullOrEmpty(vModelVO.getDriveMode())){
                vModelVO.setDriveMode(DriveModeEnum.getByValue(vModelVO.getDriveMode()).getText());
            }
            result.setData(vModelVO);
        }catch (Exception e){
            LOGGER.error("VModelController.update ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/delete")
    public RestVO<Boolean> delete(@RequestBody List<Integer> ids){

        RestVO<Boolean> result = new RestVO<>(0,"success");
        LOGGER.info("VModelController.delete params : {}", ids);

        try {
            int i =  vModelService.deleteModel(ids);
            Boolean blnrtn = i > 0;
            result.setData(blnrtn);
        }catch (Exception e){
            LOGGER.error("VModelController.delete ERROR Message : {} ", e.getMessage());
            result.setData(false);
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    @GetMapping(value = "/queryAirSuction")
    public RestVO<List<EnumVO>> queryAirSuction(){
        RestVO<List<EnumVO>> result = new RestVO<>(0,"success");
        Map<String,String> lookup = VehicleAirSuctionEnum.lookup();
        EnumUtil enumUtil =new EnumUtil();
        List<EnumVO> lists = enumUtil.getListEnumVO(lookup);
        result.setData(lists);
        return  result;
    }

    @GetMapping(value = "/queryPowType")
    public RestVO<List<EnumVO>> queryPowType(){
        RestVO<List<EnumVO>> result = new RestVO<>(0,"success");
        Map<String,String> lookup = PowerTypeEnum.lookup();
        EnumUtil enumUtil =new EnumUtil();
        List<EnumVO> lists = enumUtil.getListEnumVO(lookup);
        result.setData(lists);
        return  result;
    }

    @GetMapping(value = "/queryDriverMode")
    public RestVO<List<EnumVO>> queryDriverMode(){
        RestVO<List<EnumVO>> result = new RestVO<>(0,"success");
        Map<String,String> lookup = DriveModeEnum.lookup();
        EnumUtil enumUtil =new EnumUtil();
        List<EnumVO> lists = enumUtil.getListEnumVO(lookup);
        result.setData(lists);
        return  result;
    }


}
